﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using FileConvertPdfSystemSQLHelper;
using FileConvertPdfSystemModel;


namespace FileConvertPdfSystemDAL
{
    public class LoginUserFileDownDAL
    {
        public int  Add_LoginUserFileDown(LoginUserFileDown FileDown)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList ={ 
                 sqlHelper.CreateInParam("@File_Id",SqlDbType.Int,4, FileDown.File_ID),
                 sqlHelper.CreateInParam("@File_Size",SqlDbType.Float,8, FileDown.File_Size),
                 sqlHelper.CreateInParam("@File_name",SqlDbType.NVarChar,50,FileDown.File_Name),
                 sqlHelper.CreateInParam("@File_UploadTime",SqlDbType.DateTime,8,FileDown.File_UploadTime),
                 sqlHelper.CreateInParam("@User_Id",SqlDbType.Int,4,FileDown.User_ID),
                 sqlHelper.CreateInParam("@User_Name",SqlDbType.NVarChar,50,FileDown.User_Name)
                                      };
            try
            {
                return (sqlHelper.RunProc("Add_LoginUserFile_Down", ParamList));
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }
        public void Delete_LoginUserFileDown(int nID)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList = { sqlHelper.CreateInParam("@ID", SqlDbType.Int, 4, nID) };
            try
            {
                sqlHelper.RunProc("Delete_LoginUserFileDown", ParamList);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }


        }

        public LoginUserFileDown Get_SingleLoginUserFileDown(string UserName)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList = { sqlHelper.CreateInParam("@User_Name", SqlDbType.NVarChar, 50, UserName) };
            SqlDataReader rec = null;
            try
            {
                sqlHelper.RunProc("Get_SingleLoginUserFileDown", ParamList, out rec);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }

            LoginUserFileDown FileDown = new LoginUserFileDown();
            while (rec.Read())
            {
                
                FileDown.ID = Int32.Parse(rec["ID"].ToString());
                FileDown.File_ID = Int32.Parse(rec["File_ID"].ToString());
                FileDown.File_Size = float.Parse(rec["File_Size"].ToString());
                FileDown.File_Name = rec["File_Name"].ToString();
                FileDown.User_ID = Int32.Parse(rec["User_ID"].ToString());
            }
            return FileDown;
        }

        public DataSet Get_File(string UserName)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            string str = "select ID,File_Size,File_Name,File_UploadTime from LoginUserFileDown  where User_Name='"+ UserName + "' ORDER BY ID DESC";
            DataSet ds = null;
            try
            {
                sqlHelper.RunSQL(str, ref ds);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
            return ds;
        }

    }
}
